import requests
import json


class AssignUploadUrlTwoFalseApi:
    def __init__(self):
        pass

    def assign_upload_url(self, token, appId):
        headers = {
            "User-Agent": "RestSharp/107.3.0.0",
            "Accept": "application/json, text/json, text/x-json, text/javascript, application/xml, text/xml",
            "Content-Type": "application/json; charset=utf-8",
            "timezone": "%2b08%3a00",
            "ipv4": "192.168.43.133",
            "Accept-Language": "zh-CN,zh;q=1",
            "Authorization": f"bearer {token}"
        }
        url = "https://api.winrobot360.com/api/client/app/file/assignUploadUrl"
        data = {
            "appId": appId,
            "appType": "app",
            "version": "",
            "isBot": False
        }
        data = json.dumps(data, separators=(',', ':'))
        response = requests.post(url, headers=headers, data=data)

        try:
            json_data = response.json()
            # Decode URL-encoded characters in uploadUrl and readUrl if needed
            if 'data' in json_data and 'uploadUrl' in json_data['data']:
                from urllib.parse import unquote
                json_data['data']['uploadUrl'] = unquote(json_data['data']['uploadUrl'])
            if 'data' in json_data and 'readUrl' in json_data['data']:
                from urllib.parse import unquote
                json_data['data']['readUrl'] = unquote(json_data['data']['readUrl'])

            formatted_json = json.dumps(json_data, indent=4, ensure_ascii=False)
            # print("JSON Content:")
            # print(formatted_json)

            return json_data
        except json.JSONDecodeError:
            print("Response is not valid JSON")


if __name__ == '__main__':
    AssignUploadUrlTwoFalseApi().assign_upload_url()
